<style scoped>
.el-upload__tip {
	font-size: 9px;
}
</style>

<template>
	<div>
		<div class="container">
			<el-row style="height:40px;">
				<el-col :span="6">项目基础信息</el-col>
				<el-col :span="6" :offset="3">楼宇明细信息</el-col>
				<el-col :span="6" :offset="3">地下室间明细</el-col>
			</el-row>
			<el-row>
				<el-col :span="6">
					<el-upload
						ref="project"
						class="upload-demo"
						:http-request="uploadprj"
						:auto-upload="true"
						action
						:on-preview="handlePreview"
						:on-remove="handleRemove"
						:before-remove="beforeRemove"
						:limit="1"
						:on-exceed="handleExceed"
						:before-upload="beforeUpload"
					>
						<el-button size="small" type="primary">点击上传</el-button>
					</el-upload>
				</el-col>
				<el-col :span="6" :offset="3">
					<el-upload
						ref="building"
						class="upload-demo"
						:http-request="uploadbuild"
						:auto-upload="true"
						action
						:on-preview="handlePreview"
						:on-remove="handleRemove"
						:before-remove="beforeRemove"
						:limit="1"
						:on-exceed="handleExceed"
						:before-upload="beforeUpload"
					>
						<el-button size="small" type="primary">点击上传</el-button>
					</el-upload>
				</el-col>
				<el-col :span="6" :offset="3">
					<el-upload
						ref="basement"
						class="upload-demo"
						:http-request="uploadbase"
						:auto-upload="true"
						action
						:on-preview="handlePreview"
						:on-remove="handleRemove"
						:before-remove="beforeRemove"
						:limit="1"
						:on-exceed="handleExceed"
						:before-upload="beforeUpload"
					>
						<el-button size="small" type="primary">点击上传</el-button>
					</el-upload>
				</el-col>
			</el-row>
		</div>
		<div></div>
	</div>
</template>
<script>
export default {
	data() {
		return {};
	},
	props: {
		sourceData: {
			required: true
		}
	},
	mounted() {},
	methods: {
		inti() {},
		handleRemove(file, fileList) {},
		handlePreview(file) {},
		handleExceed(files, fileList) {
			this.$message.info("文件已上传，请删除原文件后重新上传。");
		},
		beforeRemove(file, fileList) {
			return this.$confirm(`确定移除 ${file.name}？`);
		},
		beforeUpload(file) {
			let type = file.name.split(".");
			if (type[1] === "xls" || type[1] === "xlsx") {
				return file;
			} else {
				this.$error("上传文件只能是 xls或者xlsx 格式!");
				return false;
			}
		},
		uploadprj(param) {
			let fileObj = param.file;
			let commid = this.sourceData.choseCommitId;
			let form = new FormData();
			// 文件对象
			form.append("file", fileObj);
			// 其他参数
			form.append("datakind", "prjinfo");
			form.append("commid", commid);
			this.confirmBox = this.$dialog.confirm("确定上传所选文件吗？");
			this.confirmBox.exec.then(btnName => {
				if (btnName == "ok") {
					this.upload(form);
				} else {
					this.$refs.project.clearFiles();
					this.$dialog.close(this.confirmBox.vm); // 取消删除数据操作
				}
			});
		},
		uploadbuild(param) {
			let fileObj = param.file;
			let commid = this.sourceData.choseCommitId;
			let form = new FormData();
			// 文件对象
			form.append("file", fileObj);
			// 其他参数
			form.append("datakind", "buildings");
			form.append("commid", commid);
			this.confirmBox = this.$dialog.confirm("确定上传所选文件吗？");
			this.confirmBox.exec.then(btnName => {
				if (btnName == "ok") {
					this.upload(form);
				} else {
					this.$refs.building.clearFiles();
					this.$dialog.close(this.confirmBox.vm); // 取消删除数据操作
				}
			});
		},
		uploadbase(param) {
			let fileObj = param.file;
			let commid = this.sourceData.choseCommitId;
			let form = new FormData();
			// 文件对象
			form.append("file", fileObj);
			// 其他参数
			form.append("datakind", "basement");
			form.append("commid", commid);
			this.confirmBox = this.$dialog.confirm("确定上传所选文件吗？");
			this.confirmBox.exec.then(btnName => {
				if (btnName == "ok") {
					this.upload(form);
				} else {
					this.$refs.basement.clearFiles();
					this.$dialog.close(this.confirmBox.vm); // 取消删除数据操作
				}
			});
		},
		upload(form) {
			this.$axios
				.post(
					"http://10.1.1.30:8070/rest/foru/info/infocommunity/upload",
					form,
					{
						headers: {
							"Content-Type": "multipart/form-data"
						}
					}
				)
				.then(res => {
					var data = res.data;
					if (data.result == "true") {
						this.$message({
							message: "上传成功！",
							type: "success"
						});
						this.$dialog.close(this.confirmBox.vm); // 取消删除数据操作
					} else {
						this.$message({
							message: data.resultMessage,
							type: "error"
						});
						this.$dialog.close(this.confirmBox.vm);
					}
				})
				.catch(function(error) {});
		}
	}
};
</script>
